package com.loveyukari.controller;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.model.Address;
import com.loveyukari.model.ExchangeGfit;
import com.loveyukari.model.ExchangeHistory;
import com.loveyukari.service.AddressService;
import com.loveyukari.service.AuthService;
import com.loveyukari.service.ExchangeService;
import com.loveyukari.service.ManagerService;
import com.loveyukari.support.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/exchange")
public class ExchangeController {


    @Autowired
    private ExchangeService exchangeService;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private AddressService addressService;

    @RequestMapping("/gfit/list")
    public String gfitList(Model model){
        List<ExchangeGfit> list = exchangeService.getGfitAll();
        model.addAttribute("list",list);
        return "exchange/list";
    }


    @RequestMapping(value = "/gfit/add",method = RequestMethod.GET)
    public String addGfit(){
        return "exchange/add";
    }
    @RequestMapping(value = "/gfit/add",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addGfit(ExchangeGfit exchangeGfit){
        exchangeService.addGfit(exchangeGfit);
        return new JsonResult();
    }

    @RequestMapping(value = "/gfit/update",method = RequestMethod.GET)
    public String updateGfit(Model model,int id){
        ExchangeGfit gfit = exchangeService.getGfitDetail(id);
        model.addAttribute("gfit",gfit);
        return "exchange/edit";
    }


    @RequestMapping(value = "/gfit/update",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateGfit(ExchangeGfit exchangeGfit){
        exchangeService.updateGfit(exchangeGfit);
        return new JsonResult();
    }

    @RequestMapping("/gfit/updateStatus")
    @ResponseBody
    public JsonResult updateGfitStatus(int id){
        exchangeService.updateGfitStatus(id);
        return new JsonResult();
    }


    @RequestMapping("/gfit")
    @ResponseBody
    public JsonResult historyAdd(HttpServletRequest request,int gfitId){
        String sign =(String) request.getAttribute("sign");
        int authId = managerService.getManageAuthId(sign);
        try {
            exchangeService.authExchangeGfit(authId,gfitId);
            return new JsonResult();
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        }
    }

    @RequestMapping("/history/authList")
    public String historyAuthList(HttpServletRequest request,Model model){
        String sign =(String) request.getAttribute("sign");
        int authId = managerService.getManageAuthId(sign);
        List<ExchangeHistory> list = exchangeService.getAuthHistory(authId);
        List<Address> addressList = addressService.getAuthAddress(authId);
        model.addAttribute("list",list);
        model.addAttribute("addressList",addressList);
        return "exchange/history_list";
    }


    @RequestMapping("/history/list")
    public String historyList(Model model){
        List<ExchangeHistory> list = exchangeService.getHistoryAll();
        model.addAttribute("list",list);
        return "exchange/history_list";
    }


    @RequestMapping("/history/updateAddress")
    @ResponseBody
    public JsonResult updateAddress(Integer id,Integer addressId,HttpServletRequest request){
        String sign =(String) request.getAttribute("sign");
        int authId = managerService.getManageAuthId(sign);
        exchangeService.updateAddress(id,addressId,authId);
        return new JsonResult();
    }
    @RequestMapping("/history/updateExpress")
    @ResponseBody
    public JsonResult updateExpress(Integer id,String expressNo,String expressName){
        try {
            exchangeService.updateExpress(id,expressNo,expressName);
            return new JsonResult(0,"");
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        }
    }

    @RequestMapping("/history/success")
    @ResponseBody
    public JsonResult updateStatus(Integer id,HttpServletRequest request){
        String sign =(String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            int authId = managerService.getManageAuthId(sign);
            ExchangeHistory exchangeHistory = exchangeService.getExchangeHistoryById(id);
            if (exchangeHistory == null){
                return new JsonResult(0,"");
            }
            if (authId != exchangeHistory.getAuthId()){
                return new JsonResult(1,"无权限");
            }
        }
        exchangeService.updateSuccess(id);
        return new JsonResult(0,"");
    }

}
